<template>
  <div class="card-out-container">
    <a-form layout="inline" v-if="showMore">
      <a-form-item :label="$t('hangManage.scheduledNo')">
        <a-input v-model:value.trim="queryParam.scheduleBillNo" @keyup.enter="loadData" :placeholder="$t('hangManage.scheduledNo')" />
      </a-form-item>
      <a-form-item :label="$t('hangManage.makeNo')">
        <a-select v-model:value="queryParam.moNo" :placeholder="$t('common.pleaseSelect')" allowClear>
          <a-select-option v-for="(item, index) in allMakeList" :key="index" :value="item.moNo">{{item.moNo}}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item :label="$t('hangManage.lineDate')">
        <a-range-picker v-model:value="queryParam.time" :placeholder="[$t('common.startDate'), $t('common.endDate')]" format="YYYY-MM-DD" valueFormat="YYYY-MM-DD" />
      </a-form-item>
      <a-form-item>
        <a-space>
          <a-button type="primary" @click="loadData">{{ $t('common.see') }}</a-button>
          <a-button @click="searchReset">{{ $t('common.reset') }}</a-button>
        </a-space>
      </a-form-item>
    </a-form>
    <CustomTable storeTableName="historyProdTable" rowKey="id" :loading="loading" :dataSource="dataSource" :columns="columns" :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}" :pagination="ipagination" :scroll="{y: showMore? 'calc(100vh - 395px)' : 'calc(100vh - 320px)'}" @change="handleTableChange" :showMore="showMore">
      <template #operateLeft>
        <div class="operate-left">
          <a-button type="warning" ghost :disabled="!selectedRowKeys.length" @click="handleDelete(null)">{{ $t('common.delMore') }}</a-button>
        </div>
      </template>
      <template #operateRight>
        <div class="slot-operate-right">
          <a-input-search v-model:value.trim="fuzzySearch" enter-button :placeholder="$t('hangManage.scheduledNo')" v-if="!showMore" @search="loadData" />
          <a-button class="ml-10" @click="toggleMore" type="primary" ghost>{{ $t('common.highFilter') }}</a-button>
        </div>
      </template>
      <template #bodyCell="{ scope }">
        <template v-if="scope.column.key === 'status'">
          <a-tag color="success">{{ $t('sysManage.completed') }}</a-tag>
        </template>
        <template v-else-if="scope.column.key === 'operate'">
          <span class="table-operate-container">
            <a-button type="link" class="warning-link" @click="handleDelete(scope.record.id)">{{ $t('common.del') }}</a-button> 
            <span v-if="scope.record.moStatus != 2">
              <a-divider type="vertical" />
              <a-button  type="link" class="primary-link" @click="copyMo(scope.record.id )">{{ $t('other.copy') }}</a-button>
            </span>
          </span>
        </template>
      </template>
    </CustomTable>
  </div>
</template>

<script>
import { ListMixinPm } from '@/mixin/listMixinPm.js'
import { listColumns } from './modules/historyProdConfig'
import CustomTable from '@/components/commonTable/customTable.vue'
import { store } from '@/store'
import { mapState } from 'vuex'
import { cloneDeep } from 'lodash'
import { filterObj, startTimeValue, endTimeValue } from '@/utils'
import axios from "@/utils/request"

export default {
  name: 'historyProd',
  mixins: [ListMixinPm],
  components: { CustomTable },
  computed: {
    ...mapState({
      allMakeList: state => state.normalData.allMakeList
    })
  },
  data () {
    return {
      url: {
        list: '/api/hanger/schedule-bill/list-finished',
        delete: '/api/hanger/schedule-bill/delete'
      },
      columns: listColumns,
      fuzzySearchName: 'styleNo'
    }
  },
  methods: {
    getQueryParams () {
      let param = this.showMore ? cloneDeep(this.queryParam) : {}
      if (!this.showMore) param[this.fuzzySearchName] = this.fuzzySearch
      if (this.queryParam.time && this.queryParam.time.length) {
        param.produceOnlineStartTime = startTimeValue(this.queryParam.time[0])
        param.produceOnlineEndTime = endTimeValue(this.queryParam.time[1])
      }
      delete param.time
      param.sortingFields = this.isorter.field ? [this.isorter] : null
      param.pageNo = this.ipagination.current
      param.pageSize = this.ipagination.pageSize
      return filterObj(param)
    }, 
    async copyMo(id) {
      try {
        let res = await axios.getAction('/api/hanger/schedule-bill/validate-copy-condition',{"scheduleBillId":id})
        if (res?.success){
          this.$router.push({
            path: '/hangManage/wipInfo/add',
            query: {id, type: 'copy'}
          })
        }
      }catch {
        console.log(1)
      }

    }
  },
  async created () {
    await store.dispatch('getAllMake')
  }
}
</script>